/**
	@author yangcaho message         
**/
YOUXIXIU = window.YOUXIXIU || {};
YOUXIXIU.MAIN= YOUXIXIU.MAIN || {};
YOUXIXIU.MAIN.PoupAddMblog = (function($) {
	var html='';
	var status={notice:false,atme:false,unread:false}
	var notice_num,atme_num,unread_num;
	var opt={};
	var defaults={
		id:'id',
		tpl_id:'poup_add_mblog',
		close_poup_class:"close_poup",
		add_time:0,
		album_id:0,
		reload:0,	
		title:"分享游戏秀"

	};
	//-----global functions ------// 
	function chkDefault(options)
	{
		for(key in defaults)
		{
			if(typeof(options[key])=='undefined')
			{
				options[key]=defaults[key];
			}
		}
		return options;
	}

	
	function bindclose()
	{
		$("."+opt.close_poup_class).die("click");
		$("."+opt.close_poup_class).live("click",function(e){

			close_poup();
		});
		
		
	}

	function close_poup()
	{
		$("."+opt.contain_class).remove();
		$(".sm-mask").hide();//蒙层
	}
	

	function showdetail()
	{
			$CONFIG= opt;
			$(".sm-mask").show();//蒙层
			
			
			$("#add_private_message").tmpl($CONFIG).appendTo($("body"));
			bind_click_pic();
			bind_click_submit();
			$(".W_close").die("click");
			$(".W_close").live("click",function(e){
				$("#poup_add_mblog_div").remove();
				$(".mask").hide();//蒙层
			});
	}
	function bind_click_submit()
	{
		$(".poup_add_mblog_submit").die("click");
		$(".poup_add_mblog_submit").live("click",function(e){
			

			pid = $("#poup_pid").val();
			pic = $("#poup_pic").val();
			p_width =opt.p_width;
			p_height = opt.p_height;


			content = encodeURIComponent($("#poup_content").val());
			
			if(!pid)
			{
				YOUXIXIU.MAIN.Tips.Show("图片不能为空！");
				return false;
			}
			

			$.getJSON("/interface/mblog/json_add_mblog.php?t=jsonp&album_id="+opt.album_id+"&pid="+pid+"&p_height="+p_height+"&p_width="+p_width+"&pic="+pic+"&content="+content+"&&callback=?", function(json){
			if(typeof(json)!='undefined')
			{
				if(json.error ==1)
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
				}
				else
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
					$(".mask").hide();//蒙层
					$("#poup_add_mblog_div").remove();
					if(opt.reload == 0)
					{
						location.href="/mblog/detail.php?id="+json.data.id;
					}
					else
					{
						location.reload();
					}
					
				}

				

				
				
			}
		
		});	
	
		});
	}


	function bind_click_pic()
	{
		$(".choice_pic_button").die("click");
		$(".choice_pic_button").live("click",function(e){
			
			
			$("#poup_add_mblog_pic").toggle();
				options={
				callback:'/js/uploader_callback.html',
				container:'.pic_input_file_div',
				script:'/interface/common/json_mblog_pic_upload.php',
				size:"300|300",
				thumb:1,
				onComplete:function(res){
					$("#poup_add_mblog_pic").hide();
					$("#poup_add_mblog_pic_show").find("#add_mblog_pic_review").attr("src",res.pic2);
					$("#poup_add_mblog_pic_name").html(res.pic_name);
					$("#poup_add_mblog_pic_show").show();
					
					$("#poup_pic").val(res.pic3);
					$("#poup_pid").val(res.pid );

					opt.p_width = res.p_width ;
					opt.p_height = res.p_height ;
					


				},
					errorId:'#errormsg'
				};
				YOUXIXIU.ImgUploader.init(options);
		
		});

		$("#pic_del_toggle").die("click");
		$("#pic_del_toggle").live("click",function(e){
			$("#poup_add_mblog_pic").show();
			$("#poup_add_mblog_pic_show").hide();
		});
		$("#pic_add_mblog_close").die("click");
		$("#pic_add_mblog_close").live("click",function(e){
			$("#poup_add_mblog_pic").hide();
		});
	
	}

	

	function bind_key()
	{
		$(window).keydown(function(event){
			 switch(event.keyCode) {
				case 27 : close_poup();;
		 
				}
	
		});
	
	}

	
	return {
			init: function(options)
			{
				opt=chkDefault(options);
				showdetail();
				bindclose();
				bind_key();

			},
			showdetail:function(){
				return showdetail();
			}
		};

})(jQuery);